package com.xff.dao;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.xff.Utils.MyDate;
import com.xff.bean.*;
import com.xff.dbUtils.DbUtil;

/**
 * 关于图书连接数据库的所有操作的类
 */
public class BookDao {
	/**
	 * 添加图书信息，传入所有的信息
	 * 
	 */
	public void addBook(String card ,String name ,String type , String autho ,String press, int num ,int subscribenum,String imgurl,String introduction,int price) {
		//连接数据库
		Connection conn = DbUtil.getConn();
		//SQL
		String sql = "insert into book(card,name,type,autho,press,num,subscribenum,imgurl,introduction,price) values(?,?,?,?,?,?,?,?,?,?)";
		int rs = 0;
		PreparedStatement stm = null;
		try {
			//预编译SQL，减少sql执行
			stm = conn.prepareStatement(sql);
			//添加
			stm.setString(1, card);
			stm.setString(2, name);
			stm.setString(3, type);
			stm.setString(4, autho);
			stm.setString(5, press);
			stm.setInt(6, num);
			stm.setInt(7, subscribenum);
			stm.setString(8, imgurl);
			stm.setString(9, introduction);
			stm.setInt(10, price);
			//执行更新
			rs = stm.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	/**
	 * 获取所有的图书信息，返回的是ArrayList数组形式
	 * 访问 ArrayList 中的元素可以使用 get() 方法：
	 *修改 ArrayList 中的元素可以使用 set() 方法：
	 *删除 ArrayList 中的元素可以使用 remove() 方法：
	 */
	public ArrayList<BookBean> get_ListInfo(){
		ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from book";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				BookBean tag = new BookBean();
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setNum(rs.getInt("num"));
				tag.setSubscribenum(rs.getInt("subscribenum"));
				tag.setImgurl(rs.getString("imgurl"));
				tag.setIntroduction(rs.getString("introduction"));
				tag.setPrice(rs.getInt("price"));
				tag_Array.add(tag);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}
	/**
	 * 获取借阅记录的全部信息，传入的条件有status，aid，表示搜索正在借阅的，或者已经还书的信息，aid代表当前登录用户
	 *
	 */
	public ArrayList<HistoryBean> get_HisoryListInfo(int status,String aid){
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where aid = '"+ aid +"' and status = '"+ status+"'";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 获取借阅记录的全部信息，传入的条件有status，tip，表示搜索正在借阅的，或者已经还书的信息
	 *
	 */
	public ArrayList<HistoryBean> get_HisoryListInfo2(int tip,String username){
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DbUtil.getConn();
		String sql="select * from history where adminname = '"+ username +"'";
		if(tip==1)//正常、超期
			sql = "select * from history where adminname = '"+ username +"' and status IN (1,4)";
		if(tip==2)//已还、挂失
			sql = "select * from history where adminname = '"+ username +"' and status IN (0,3)";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);

			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}
	/**
	 * 获取借阅记录的全部信息，传入的条件有status，表示搜索正在借阅的，或者已经还书的信息
	 *
	 */
	public ArrayList<HistoryBean> get_HistoryListInfo2(int status){
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where status='"+status+"'";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	public ArrayList<HistoryBean> get_HistoryListInfo3(int status1,int status2){
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where status IN ("+status1+","+status2+")";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/**
	 * 获取借阅记录，传入的条件有name，tip，表示搜索相关图书名称和读者账号/姓名的记录
	 *
	 */
	public ArrayList<HistoryBean> get_BorrowListInfo(int tip,String name,int status1,int status2){
		ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();
		Connection conn = DbUtil.getConn();
		String sql="select * from history";
		if(tip==1)//根据书名查询
			sql = "select * from history where bookname like '%"+ name +"%' AND status IN ("+status1+","+status2+")";
		if(tip==2)//根据用户查询
			sql = "select * from history where status IN ("+status1+","+status2+") AND (adminname like '%"+ name +"%' or username like '%"+name+"%')";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while (rs.next()) {
				HistoryBean tag = new HistoryBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag_Array.add(tag);

			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/*
	*
	* 获取罚款记录
	* */
	public ArrayList<HistoryPenaltyBean> get_HistoryPenaltyListInfo(int tip){
		ArrayList<HistoryPenaltyBean> tag_Array = new ArrayList<HistoryPenaltyBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where penaltystatus IS NOT NULL AND penaltystatus>0";
		if(tip==1){//已交完
			sql="select * from history where penaltystatus=1";
		}
		if(tip==2){//还有未交
			sql="select * from history where penaltystatus=2";
		}
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				HistoryPenaltyBean tag = new HistoryPenaltyBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag.setPenaltystatus(rs.getInt("penaltystatus"));
				tag.setHaspaid(rs.getInt("haspaid"));
				tag.setNopaid(rs.getInt("nopaid"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	public ArrayList<HistoryPenaltyBean> get_HistoryPenaltyListInfo2(int status){
		ArrayList<HistoryPenaltyBean> tag_Array = new ArrayList<HistoryPenaltyBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where status="+status;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				HistoryPenaltyBean tag = new HistoryPenaltyBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag.setPenaltystatus(rs.getInt("penaltystatus"));
				tag.setHaspaid(rs.getInt("haspaid"));
				tag.setNopaid(rs.getInt("nopaid"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	public HistoryPenaltyBean get_HistoryPenaltyListInfo3(int hid){
		HistoryPenaltyBean tag = new HistoryPenaltyBean();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where hid="+hid;
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag.setPenaltystatus(rs.getInt("penaltystatus"));
				tag.setHaspaid(rs.getInt("haspaid"));
				tag.setNopaid(rs.getInt("nopaid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag;
	}

	/*
	* 查询罚款，根据用户名
	* */
	public ArrayList<HistoryPenaltyBean> get_HistoryPenaltyListInfo(String name){
		ArrayList<HistoryPenaltyBean> tag_Array = new ArrayList<HistoryPenaltyBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from history where penaltystatus IS NOT NULL AND penaltystatus>0 AND (adminname like '%" + name+"%' OR username like '%" + name+"%') ";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				HistoryPenaltyBean tag = new HistoryPenaltyBean();
				tag.setHid(rs.getInt("hid"));
				tag.setAid(rs.getInt("aid"));
				tag.setBid(rs.getInt("bid"));
				tag.setBookname(rs.getString("bookname"));
				tag.setCard(rs.getString("card"));
				tag.setAdminname(rs.getString("adminname"));
				tag.setUsername(rs.getString("username"));
				tag.setBegintime(rs.getString("begintime"));
				tag.setEndtime(rs.getString("endtime"));
				tag.setStatus(rs.getInt("status"));
				tag.setPenaltystatus(rs.getInt("penaltystatus"));
				tag.setHaspaid(rs.getInt("haspaid"));
				tag.setNopaid(rs.getInt("nopaid"));
				tag_Array.add(tag);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}

	/*
	* 缴费
	* */
	public void penalty(int hid,int nopaid,int haspaid){
		Connection conn = DbUtil.getConn();
		String sql = "update history set penaltystatus=?,haspaid=?,nopaid=? where hid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, 1);
			stm.setInt(2, haspaid+nopaid);
			stm.setInt(3, 0);
			stm.setInt(4, hid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String get_Bookimgurl(int bid){
		Connection conn = DbUtil.getConn();
		String sql = "select imgurl from book where bid = '"+ bid +"' ";
		PreparedStatement stm = null;
		ResultSet rs = null;
		String imgurl=null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {
				imgurl=rs.getString("imgurl");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return imgurl;
	}
	/**
	 * 获取单个图书的信息，根据传入的bid来查找，返回一个BookBean数据类型
	 *
	 */
	public BookBean get_BookInfo(int bid) {
		BookBean tag = new BookBean();
		Connection conn = DbUtil.getConn();
		String sql = "select * from book where bid = '"+ bid +"' ";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()) {
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setNum(rs.getInt("num"));
				tag.setSubscribenum(rs.getInt("subscribenum"));
				tag.setImgurl(rs.getString("imgurl"));
				tag.setIntroduction(rs.getString("introduction"));
				tag.setPrice(rs.getInt("price"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag;
	}
	/**
	 * 修改图书的信息，bid作为条件，
	 */
	public void updateBook(int bid, String card, String name, String type, String autho, String press, int num,int subscribenum,String imgurl,int price) {
		// TODO Auto-generated method stub
		Connection conn = DbUtil.getConn();
		String sql = "update book set name=?,card=?,type=?,autho=?,press=?,num=?,subscribenum=?,imgurl=?,price=? where bid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, name);
			stm.setString(2, card);
			stm.setString(3, type);
			stm.setString(4, autho);
			stm.setString(5, press);
			stm.setInt(6, num);
			stm.setInt(7, subscribenum);
			stm.setString(8, imgurl);
			stm.setInt(9, price);
			stm.setInt(10, bid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateBook(BookBean bookBean) {
		// TODO Auto-generated method stub
		Connection conn = DbUtil.getConn();
		String sql = "update book set name=?,card=?,type=?,autho=?,press=?,num=?,subscribenum=?,imgurl=?,price=? where bid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, bookBean.getName());
			stm.setString(2, bookBean.getCard());
			stm.setString(3, bookBean.getType());
			stm.setString(4, bookBean.getAutho());
			stm.setString(5, bookBean.getPress());
			stm.setInt(6, bookBean.getNum());
			stm.setInt(7, bookBean.getSubscribenum());
			stm.setString(8, bookBean.getImgurl());
			stm.setInt(9, bookBean.getPrice());
			stm.setInt(10, bookBean.getBid());
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 删除图书信息，根据传入的bid作为条件
	 */
	public void deleteBook(int bid) {
		Connection conn = DbUtil.getConn();
		String sql = "delete from book where bid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, bid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 用户查找图书，根据输入的名称，使用like进行模糊查询，然后返回一个ArrayList数组类型
	 */
	public ArrayList<BookBean> getLikeList(String name){
		ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();
		Connection conn = DbUtil.getConn();
		String sql = "select * from book where name like '%" + name+"%'";
		PreparedStatement stm = null;
		ResultSet rs =null;
		try {
			stm = conn.prepareStatement(sql);
			rs = stm.executeQuery();
			while(rs.next()){
				BookBean tag = new BookBean();
				tag.setBid(rs.getInt("bid"));
				tag.setName(rs.getString("name"));
				tag.setCard(rs.getString("card"));
				tag.setType(rs.getString("type"));
				tag.setAutho(rs.getString("autho"));
				tag.setPress(rs.getString("press"));
				tag.setNum(rs.getInt("num"));
				tag.setSubscribenum(rs.getInt("subscribenum"));
				tag.setImgurl(rs.getString("imgurl"));
				tag.setIntroduction(rs.getString("introduction"));
				tag.setPrice(rs.getInt("price"));
				tag_Array.add(tag);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			DbUtil.CloseDB(rs, stm, conn);
		}
		return tag_Array;
	}
	/**
	 * 图书借阅函数，根据传入bid图书id，adminbean当前登录用户的信息，在借阅记录数据表中新插入一条记录
	 *
	 */
	public void borrowBook(int bid, AdminBean adminbean) {
		BookBean bookbean = new BookBean();
		bookbean = this.get_BookInfo(bid);
		//生成借阅开始时间
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String begintime=format.format(new java.util.Date());


		//生成截至还书日期
		String endtime = MyDate.plusNowDay(adminbean.getLend_num());

		Connection conn = DbUtil.getConn();
		String sql = "insert into history(aid,bid,card,bookname,adminname,username,begintime,endtime,status) values(?,?,?,?,?,?,?,?,?)";
		PreparedStatement stm = null;
		int rs = 0;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1, adminbean.getAid());
			stm.setInt(2, bookbean.getBid());
			stm.setString(3, bookbean.getCard());
			stm.setString(4, bookbean.getName());
			stm.setString(5, adminbean.getUsername());
			stm.setString(6, adminbean.getName());
			stm.setString(7, begintime);
			stm.setString(8, endtime);
			stm.setInt(9, 1);
			rs = stm.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	/**
	 * 还书功能的函数，根据传入的hid借阅记录id，讲status字段的值改为0，并将还书日期改变为当前日期
	 * 
	 */
	public void borrowBook2(int hid) {
		//生成还书日期
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String endtime=format.format(new java.util.Date());

		Connection conn = DbUtil.getConn();
		String sql = "update history set endtime=?,status=? where hid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, endtime);
			stm.setInt(2, 0);
			stm.setInt(3, hid);
			stm.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}


	/*
	* 挂失书籍
	* */
	public void losebook(int hid,int price) {
		Connection conn = DbUtil.getConn();
		String sql = "update history set endtime=?,status=?,penaltystatus=?,nopaid=nopaid+? where hid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);

			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date=format.format(new java.util.Date());

			stm.setString(1, date);
			stm.setInt(2, 3);
			stm.setInt(3, 2);
			stm.setInt(4, price);
			stm.setInt(5, hid);

			stm.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/*
	* 修改某类书的类型
	* */
	public void updateBooks_type(String oldtype,String newtype) {
		// TODO Auto-generated method stub
		Connection conn = DbUtil.getConn();
		String sql = "update book set type=? where type=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, newtype);
			stm.setString(2, oldtype);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	* 超期修改
	* */
	public void overtime(int hid,int times) {
		// TODO Auto-generated method stub
		Connection conn = DbUtil.getConn();
		String sql = "update history set status=?,penaltystatus=?,nopaid=nopaid+? where hid=?";
		PreparedStatement stm = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setInt(1,4);
			stm.setInt(2,2);
			stm.setInt(3,5*times);
			stm.setInt(4,hid);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public void updateHistoryPenalty(HistoryPenaltyBean bean) {
		// TODO Auto-generated method stub
		Connection conn = DbUtil.getConn();
		String sql = "update history set endtime=?,status=?,penaltystatus=?,haspaid=?,nopaid=? where hid=?";
		PreparedStatement stm = null;
		int rs = 0;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1,bean.getEndtime());
			stm.setInt(2,bean.getStatus());

			stm.setInt(3,bean.getPenaltystatus());

			stm.setInt(4,bean.getHaspaid());
			stm.setInt(5,bean.getNopaid());
			stm.setInt(6,bean.getHid());
			rs = stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
